home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / GParted Live CD / Bin / gparted-livecd-0.2.2.iso / fake / needwrite / etc / rc.d / init.d / functions < prev    next >
Encoding:
Text File  |  2006-01-27  |  5.4 KB  |  275 lines

  1. #!/bin/sh
  2. # Begin $rc_base/init.d/functions - Run Level Control Functions
  3.  
  4. # Based on functions script from LFS-3.1 and earlier.
  5. # Rewritten by Gerard Beekmans  - gerard@linuxfromscratch.org
  6.  
  7. # With code based on Matthias Benkmann's simpleinit-msb @
  8. # http://winterdrache.de/linux/newboot/index.html
  9.  
  10. # Initial Stop Signal
  11. STOPSIG=TERM
  12.  
  13. # The fallback if STOPSIG does not stop a process in time (see KILLDELAY)
  14. FALLBACK=KILL
  15.  
  16. # Signal sent to running processes to refresh their configuration
  17. RELOADSIG=HUP
  18.  
  19. # Number of seconds between STOPSIG and FALLBACK when stopping processes
  20. KILLDELAY=3
  21.  
  22. umask 022
  23. export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
  24.  
  25. # Width of the Screen
  26. COLUMNS=$(stty size)
  27. COLUMNS=${COLUMNS##* }
  28. # When using remote connections, such as a serial port, stty size returns 0
  29. if [ "$COLUMNS" = "0" ]; then COLUMNS=80; fi
  30.  
  31. # Measurements for positioning result messages
  32. COL=$(($COLUMNS - 10))
  33. WCOL=$(($COLUMNS - 30))
  34.  
  35. # Set Cursur Position Commands, used via echo -e
  36. SET_COL="\\033[${COL}G"
  37. SET_WCOL="\\033[${WCOL}G"
  38. CURS_UP="\\033[A"
  39.  
  40. # Set color commands, used via echo -e
  41. NORMAL="\\033[0;39m"
  42. SUCCESS="\\033[1;32m"
  43. WARNING="\\033[1;33m"
  44. FAILURE="\\033[1;31m"
  45.  
  46. echo_ok()
  47. {
  48.     echo -e "$CURS_UP$SET_COL"["$SUCCESS""  OK  ""$NORMAL"]
  49. }
  50.  
  51. echo_failure()
  52. {
  53.     echo -e "$CURS_UP$SET_COL"["$FAILURE"FAILED"$NORMAL"]
  54. }
  55.  
  56. echo_warning()
  57. {
  58.     echo -e "$CURS_UP$SET_WCOL$@$SET_COL"["$WARNING" WARN  "$NORMAL"]
  59. }
  60.  
  61. # $i is inherited by the rc script
  62. print_error_msg()
  63. {
  64.     echo -e -n $FAILURE
  65.     echo
  66.     echo "You should not be reading this error message. It means"
  67.     echo "that an unforseen error took place in $i,"
  68.     echo "which exited with a return value of $error_value"
  69.     echo
  70.     echo "If you're able to track this error down to a bug in one"
  71.     echo "of the files provided by the LFS book, please be so kind"
  72.     echo "to inform us at lfs-dev@linuxfromscratch.org."
  73.     echo -e -n $NORMAL
  74.     echo
  75.     echo
  76.     echo "Press Enter to continue..."
  77.     read ENTER
  78. }
  79.  
  80. # $i is inherited by the rc script
  81. check_script_status()
  82. {
  83.     if [ ! -f $i ];    then
  84.         echo "$i is not a valid symlink"
  85.         continue
  86.     fi
  87.  
  88.     if [ ! -x $i ]; then
  89.         echo "$i is not executable, skipping"
  90.         continue
  91.     fi
  92. }
  93.  
  94. evaluate_retval()
  95. {
  96.     error_value=$?
  97.  
  98.     if [ $error_value = 0 ]; then
  99.         print_status success
  100.     else
  101.         print_status failure
  102.         sleep 5
  103.     fi
  104.  
  105.     return 0
  106.     #return $error_value
  107. }
  108.  
  109. print_status()
  110. {
  111.         if [ $# = 0 ]; then
  112.                 echo "Usage: $0 {success|warning|failure}"
  113.                 return 1
  114.         fi
  115.  
  116.         case "$1" in
  117.                 success)
  118.                         echo_ok
  119.                 ;;
  120.                 warning)
  121.                         case "$2" in
  122.                                 running)
  123.                                         echo_warning "Already running"
  124.                                 ;;
  125.                                 not_running)
  126.                                         echo_warning "Not running"
  127.                                 ;;
  128.                                 not_available)
  129.                                         echo_warning "Not available"
  130.                                 ;;
  131.                         esac
  132.                 ;;
  133.                 failure)
  134.                         echo_failure
  135.                 ;;
  136.         esac
  137. }
  138.  
  139. # Returns all of the pid #'s for $1 process
  140. getpids()
  141. {
  142.     base=${1##*/}
  143.     local lpids=""
  144.     local pid
  145.  
  146.     pidlist=""
  147.     if [ -n "$PIDFILE" ]; then
  148.         lpids=$(cat $PIDFILE 2>/dev/null)
  149.     else
  150.         lpids=$(pidof $base)
  151.     fi
  152.  
  153.     for pid in $lpids
  154.     do
  155.         if [ $pid -ne $$ ] && [ $pid -ne $PPID ]
  156.         then
  157.             pidlist="$pidlist $pid"
  158.         fi
  159.     done
  160. }
  161.  
  162. # Starts a program if it is currently not running
  163. loadproc()
  164. {
  165.     if [ $# = 0 ]; then
  166.         echo "Usage: loadproc {program}"
  167.         exit 1
  168.         fi
  169.  
  170.     getpids $1
  171.  
  172.     if [ -z "$pidlist" ]; then
  173.         "$@"
  174.         evaluate_retval
  175.     else
  176.         print_status warning running
  177.     fi
  178. }
  179.  
  180. # Stops a process if it is running
  181. killproc()
  182. {
  183.     if [ -z "$PIDFILE" -a -z "$#" ]; then
  184.         echo "Usage: killproc [{program}]"
  185.         exit 1    
  186.     fi
  187.  
  188.     getpids "$1"
  189.  
  190.     if [ -n "$pidlist" ]; then
  191.         local i=0
  192.         for pid in $pidlist; do
  193.             kill -$STOPSIG $pid 2>/dev/null
  194.             while [ $i -lt $KILLDELAY ]; do
  195.                 kill -0 $pid 2>/dev/null || break
  196.                 sleep 1
  197.                 i=$(($i+1))
  198.             done
  199.  
  200.             if [ -n "$FALLBACK" ]; then
  201.                 kill -$FALLBACK $pid 2>/dev/null
  202.             fi
  203.         done
  204.  
  205.         getpids "$1"
  206.  
  207.         base=${1##*/}
  208.  
  209.         if [ -n "$pidlist" ]; then
  210.             failure=1
  211.         else
  212.             failure=0
  213.             if [ -n "$PIDFILE" ]; then
  214.                 rm -f $PIDFILE
  215.             else
  216.                 rm -f /var/run/$base.pid
  217.             fi
  218.         fi
  219.  
  220.         (exit $failure)
  221.         evaluate_retval
  222.     else
  223.         print_status warning not_running
  224.     fi
  225. }
  226.  
  227. reloadproc()
  228. {
  229.     if [ $# = 0 ]; then
  230.         echo "Usage: reloadproc [{program}]"
  231.         exit 1
  232.     fi
  233.  
  234.     getpids $1
  235.  
  236.     if [ -n "$pidlist" ]; then
  237.         failure=0
  238.  
  239.                 for pid in $pidlist
  240.                 do
  241.                         kill -$RELOADSIG $pid || failure=1
  242.                 done
  243.  
  244.                 (exit $failure)
  245.  
  246.         evaluate_retval
  247.         else
  248.                 print_status warning not_running
  249.         fi
  250. }
  251.  
  252. statusproc()
  253. {
  254.         if [ $# = 0 ]; then
  255.                 echo "Usage: statusproc {program}"
  256.                 exit 1
  257.         fi
  258.  
  259.         base=${1##*/}
  260.         getpids $base
  261.  
  262.         if [ -n "$pidlist" ]; then
  263.                 echo "$base is running with Process ID(s) $pidlist"
  264.         else
  265.                 if [ -s /var/run/$base.pid ]; then
  266.                         echo "$1 is not running but /var/run/$base.pid exists"
  267.                         return 1
  268.                 else
  269.                         echo "$1 is not running"
  270.                 fi
  271.         fi
  272. }
  273.  
  274. # End $rc_base/init.d/functions
  275.